<?php

namespace App\Http\Controllers\v1;


use App\Models\Area;
use App\Models\Family;
use App\Models\Project;
use App\Models\SmsCode;
use App\Models\Team;
use App\Models\User;
use App\Models\UserCollection;
use App\Models\UserInterface;
use App\Models\UserMessage;
use App\Models\Volunteer;
use App\Models\VolunteerGoodAt;
use EasyWeChat\Core\Exception;
use Illuminate\Http\Request;
use App\Libs\IdCard;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Hashids\Hashids;

class MessageController extends Controller
{

    public function listInfo(Request $request)
    {
        $userInfo = User::where('type', 1)->where('token', $this->getToken())->first();

        $userInterface = UserInterface::where('type', 1)->where('user_id', $userInfo->id)->first();
        if(empty($userInterface))
        {
            $userInterface = new UserInterface();
            $userInterface->fill(['user_id'=>$userInfo->id, 'type'=>1]);
        }
        $userInterface->fill(['last_time'=>time()]);
        $userInterface->save();

        $pageNum = request()->input('current_page', 1);
        $pageSize = request()->input('page_size', 10);
        $from = ($pageNum - 1)*$pageSize;

        $nodes = UserMessage::with('user');
        $nodes = $nodes->where(function($query) use ($userInfo){
                    $query->where(function ($q) use ($userInfo){
                        $q->where('type', 0)->where('publish_time', '>', $userInfo->created_at);
                    })->orwhere(function($q)  use ($userInfo){
                        $q->where('type', 1)->where('to_user_id', $userInfo->id);
                    } );
            });

        $count = $nodes->count();
        $nodes = $nodes->orderBy('publish_time','desc');

        $lists = $nodes->take($pageSize)->skip($from)->get();

        $pageInfo = [
            "current_page" => intval($pageNum),
            "total_page" => ceil($count / $pageSize),
            "page_size" => intval($pageSize)
        ];

        $list = [];
        foreach($lists as $msg)
        {
            $tmp = UserMessage::format($msg);


            switch ($msg->user->type)
            {
                case 1:
                    $tmp['username'] = $msg->user->username;
                    $tmp['avatars'] = $msg->user->avatars;
                    break;

                case 2:
                    $tmp['username'] = $msg->user->team->name;
                    $tmp['avatars'] = $msg->user->team->logo;
                    break;

                case 3:
                    $tmp['username'] = $msg->user->org->name;
                    $tmp['avatars'] = $msg->user->org->logo;
                    break;

                case 4:
                    $tmp['username'] = '志多星';
                    $tmp['avatars'] = $msg->user->avatars;
                    break;
            }
            $tmp['avatars'] = image_url($tmp['avatars']);
            $tmp['is_new'] = $msg->created_at > date('Y-m-d H:i:s',  $userInterface->last_time)? 1:0;
            $list[] = $tmp;
        }

        $data['list'] = $list;
        $data['page'] = $pageInfo;
        $this->setData($data);

        return $this->response();
    }
}
